﻿
var gdate = "";
var date = moment().format("YYYY-MM-DD");
var now = moment().format("YYYY-MM-DD");
var hour = parseInt(moment().format("H"));
  
    var today = moment().format("YYYY-MM-DD");
    var yesterday = moment().add("days", -1).format("YYYY-MM-DD");
    var day = moment().format('d');
  //  console.log(day);
   
    if (day == 0) {
        date = moment().add("days", -2).format("YYYY-MM-DD");
    } else if(day==6) {
        date = moment().add("days", -1).format("YYYY-MM-DD");
    }
 
    
function LoadCompanyList(date) {
    var url = host + "/longhu/list?date=" + date;
    $.ajax({
        type: "GET",
        url: url,
        cache: false,
        success: function (data1) {
            //console.dir(data1);
            if (data1.data == null) { alert("加载数据为空！"); return false; }
            var shList = data1.data.SH;
            var szList = data1.data.SZ;
            date = data1.data.day;
            $(".Wdate").val(date);
            gdate = data1.data.day;
            var firstSecurity = shList.length > 0 ? shList[0] : szList.length > 0 ? szList[0] : null;
            if (firstSecurity != null) {
                LoadSecurityList(firstSecurity.stockName, firstSecurity.code, date);
            }
            $("#dvCompanyList").children().remove();
            var content = template('templateCompanyList', data1.data);
            $("#dvCompanyList").append(content);
        } 
    });
}

function LoadSecurityList(name, code, date) {
    var url = host + "/longhu/detail?date=" + date + "&code=" + code;
    $.ajax({
        type: "GET",
        url: url,
        cache: false,
        success: function (data2) {
            //console.dir(data2);
            if (data2.data == null) return false;

            $("#dvContent").find("table,h4").remove();
            var content = "";
            var h4 = "<h4>$1</h4>";
            var head = " <table style='width: 100%;margin-top: 20px'><thead><tr class='bg-blue'><th width='63%' class='tl'style='text-align: left'>$$金额最大的前5名营业部</th><th>买入额/万</th><th>卖出额/万</th><th>净额/万</th></tr></thead><tbody>";
            var foot = "</tbody></table> ";
            for (var i in data2.data) {
                var title = GetReason(parseInt(i));
                title = name + "(" + code + ")明细：" + title;
                //console.log(title);

                var obj = data2.data[i];
           
                content += h4.replace("$1", title);
                content += head.replace("$$", "买入");
                $(obj[0]).each(function (key, val) {
                    var amount = Math.round((parseFloat(val.buyAmount.toFixed(2)) - parseFloat(val.sellAmount.toFixed(2))) * 100) / 100;
                    var ts = "";
                    if (val.investor != "") {
                        ts = "<span class='ts'>" + val.investor + "<span>";
                    }
                    content += "<tr> <td class='tl rel'> <a href='javascript:;' target='_blank' class='details' title='" + val.seatName + "'>" + val.seatName + "</a>"+ts+"</td><td class='c-rise tr rel'>" + (val.buyAmount == 0 ? '-' : val.buyAmount) + "</td><td class='c-fall tr rel'>" + (val.sellAmount == 0 ? '-' : val.sellAmount) + "</td><td class='c-rise tr rel'>" + amount + "</td></tr>";
                })
                content += foot;

                content += head.replace("$$", "卖出")
                $(obj[1]).each(function (key, val) {
                    var amount = Math.round((parseFloat(val.buyAmount.toFixed(2)) - parseFloat(val.sellAmount.toFixed(2))) * 100) / 100;
                    var ts = "";
                    if (val.investor != "") {
                        ts = "<span class='ts'>" + val.investor + "<span>";
                    }
                    content += "<tr> <td class='tl rel'> <a href='javascript:;' target='_blank' class='details' title='" + val.seatName + "'>" + val.seatName + "</a>"+ts+"</td><td class='c-rise tr rel'>" + (val.buyAmount == 0 ? '-' : val.buyAmount) + "</td><td class='c-fall tr rel'>" + (val.sellAmount == 0 ? '-' : val.sellAmount) + "</td><td class='c-rise tr rel'>" + amount + "</td></tr>";
                })
                content += foot;
            }
            //console.log(content);
            $("#dvContent").append(content);
        } 
    });
}
LoadCompanyList(date);

$(document).on("click", "#dvTopSH span,#dvTopSZ span", function () {
    var code = $(this).find("a").attr("stockcode");
    var name = $(this).find("a").html();
    //console.log(date);
 
    LoadSecurityList(name, code, gdate);
});
function GetReason(type) {
        var result = "";
        switch (type) {
            case 1: result = "日涨幅偏离值达7%的证券"; break;
            case 2: result = "日跌幅偏离值达7%的证券"; break;
            case 3: result = "日振幅值达15%的证券"; break;
            case 4: result = "日换手率达20%的证券"; break;
            case 5: result = "连续三个交易日内,涨幅偏离值累计达12%的ST和*ST证券"; break;
            case 6: result = "连续三个交易日内,跌幅偏离值累计达12%的ST和*ST证券"; break;
            case 8: result = "连续三个交易日内,涨幅偏离值累计达20%的证券"; break;
            case 7: result = "连续三个交易日内,跌幅偏离值累计达20%的证券"; break;
            case 9: result = "连续三个交易日内,涨幅偏离值累计达15%的ST和*ST证券"; break;
            case 10: result = "连续三个交易日内,跌幅偏离值累计达15%的ST和*ST证券"; break;
            default: break;
        }
        return result;
}

$(document).on("click", "#btnQuery", function () {
    var date = $(".Wdate").val();
    LoadCompanyList(date);
})
 
function LoadInfoList() {
    var url = host + "/event/list";
    $.ajax({
        type: "GET",
        url: url,
        cache: false,
        success: function (data) {
            //console.dir(data);
            var str = "";
            if (data!=null&&data.data!=null&&data.data.length > 0) {
                str += "<tr>";
                $(data.data).each(function (key,val) {
                    str += "<td><span class='span2'>"+val.eventDesc+"</span><span class='span2'>"+val.periodDesc+"</span></td>";
                })
            }
            $("#tbInfoList").append(str);
        },
        error: function (err) {
            //console.dir(err);
        }
    });
}
var totalPage = 0;
var pageSize=10;
var dtd = $.Deferred();
function GetTotalCount(dtd) {
    var url = newsHost + "/topicNews/list";
    var obj = {};
 
    obj.PageIndex =   1;
    obj.PageSize = 1;
    $.ajax({
        type: "GET",
        url: url,
        data: obj,
        cache: false,
        success: function (data) {
            if (data != null && data.data != null && data.data.length > 0) {
                var totalCount = data.data[0].totalCount;
                totalPage = Math.ceil(totalCount / pageSize);
            }
            dtd.resolve();
        }
    });
    return dtd;
}

function LoadNewsList(current) {
    var url = newsHost + "/topicNews/list";
    var obj = {};
    obj.PageIndex = current + 1;
    obj.PageSize = pageSize;
    $.ajax({
        type: "GET",
        url: url,
        data:obj,
        cache: false,
        success: function (data) {
            
            if (data != null && data.data != null && data.data.length > 0) {
                $("#divNewsList").html("");
                totalCount = data.data[0].totalCount;
                var content = template('templateNewsList', data);
                $("#divNewsList").append(content);
            }
        },
        error: function (err) {
            //console.dir(err);
        }
    });
}

LoadInfoList();


$(function () {
    var initPagination = function () {
        $.when(GetTotalCount(dtd)).done(function () {
            $("#newsPage").pagination(totalPage, {
                num_edge_entries: 2,
                //边缘页数
                num_display_entries: 4,
                //主体页数
                callback: LoadNewsList,
                items_per_page: 1,
                //每页显示1项
                prev_text: "Prev",
                next_text: "Next"
            });
        });
    }();
});


 